define(["moment"], function (moment) {
	return ["$scope", "$filter", "$stateParams", "config", "MMWService","$mdDialog",
	function ($scope, $filter, $stateParams, config, $MMWService,$mdDialog) {
		$scope.init = function(){
			$scope.querylist = {
				op_no:''
			};
			$scope.itemList= [];
			$scope.selectlist = {
				current_index:-1,
				current_item:{}
			};
		}
		$scope.back = function(){
           
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);//動畫
		}
		
		$scope.confirm = function(){
		
			$scope.openRework();
			//$scope.$root.hideMenu();
			//$scope.$root.changeProgram('home', true); 
		}
		$scope.loadOP = function()
		{
			$MMWService.showQueryListPNIG({
                page_mode: 'op_with_waiting_lot_get',
                condition: [
                    {name:$filter('translate')('common.queryList.op_c'), column_name:'a.OPNo', query_mode:'2', value:$scope.querylist.op_no}
                ],
                header: [
                    {label:$filter('translate')('common.queryList.op_c'), code:'op_no'},
                    {label:$filter('translate')('common.queryList.op_n'), code:'op_name'}
                ],
                selectCode: $scope.querylist.op_no,
                confirm : function(selectItem){
                    $scope.querylist.op_no = selectItem.op_no;
                }
            });
		}
		$scope.search = function()
		{
			if($scope.querylist.op_no == '')
			{
				$scope.$root.showAlert($filter('translate')('B0302.alert.msg1'));
				return;		
			}
			
			//re set selectlist
			$scope.selectlist.current_index = -1;
			$scope.selectlist.current_item = {};	
			var content = 
			{
				page_mode:'rework_judge_info_get',
				query_condition_info:[
					{
						name:$filter('translate')('common.queryList.op_c'), 
						column_name:'a.OPNo', 
						query_mode:'0', 
						value:$scope.querylist.op_no
					}
				]
			};
			$MMWService.sendToServer({
                uri: 'ServicesSTD.Module_SYS.page_number_info_get',
                content: content,
				success: function (data) 
				{
					$scope.itemList = [];
					if(data.page_number_info_get.length > 0)
					{
						//init list and conversion data for submit
						for(var i in data.page_number_info_get)
						{
							var tmp_obj = {};
							tmp_obj.item_name = data.page_number_info_get[i].productname;
							tmp_obj.qty = data.page_number_info_get[i].createqty;
							tmp_obj.item_no = data.page_number_info_get[i].productno;
							tmp_obj.plot_no = data.page_number_info_get[i].lotno;
							tmp_obj.op_no = data.page_number_info_get[i].op_no;
							tmp_obj.plot_stamp = data.page_number_info_get[i].plot_stamp;
							tmp_obj.status = data.page_number_info_get[i].status;
							tmp_obj.machine_no = data.page_number_info_get[i].equipmentno;
							$scope.itemList.push(tmp_obj);
						}
					}
					else
						$scope.$root.showAlert($filter('translate')('common.msg.no_data'));	
				},
				error:function(data)
				{
					$scope.$root.showAlert(data.Message);
				}
            });
		}
		$scope.selectAction = function(index,item)
		{
			if($scope.selectlist.current_index == index)
			{
				$scope.selectlist.current_index = -1;
				$scope.selectlist.current_item = {};
			}
			else
			{
				$scope.selectlist.current_index = index;
				$scope.selectlist.current_item = item;	
			}
			
		}
		$scope.openRework = function()
		{
			if($scope.selectlist.current_index == -1 || Object.keys($scope.selectlist.current_item).length == 0)
			{
				$scope.$root.showAlert($filter('translate')('B0302.alert.msg2'));
				return;
			}
			$mdDialog.serviceDialog('B0213Service',{
				plot_detail : [$scope.selectlist.current_item],
				option : 0,
				machine_no:$scope.selectlist.current_item.machine_no,
				parent : 'B0302',
				feedback:function(){
					$scope.search();
				}
			});
		}
		
	}
]});